package com.cn.transfer.jobhandler.purchaserequisition;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.cn.transfer.common.constant.KeyConstant;
import com.cn.transfer.common.entity.VvPuPurchaseRequisition;
import com.cn.transfer.common.entity.VvPuPurchaseRequisitionB;
import com.cn.transfer.common.entity.VvSaSaleOrderB;
import com.cn.transfer.common.entity.jiandaoyun.JdyPurchaseOrder;
import com.cn.transfer.common.http.ApiClient;
import com.cn.transfer.common.utils.ConnmeUtils;
import com.cn.transfer.handle.impl.purchase.community.PurchaseCommunity;
import com.cn.transfer.mapper.VvPuPurchaseRequisitionBMapper;
import com.cn.transfer.mapper.VvPuPurchaseRequisitionMapper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: 向伟典
 * @date: 2023/9/21 11:33
 * @Description: 新增两年请购单到简道云
 */
@Component
public class PurchaseRequisitionAddXxlJob {
    @Autowired
    private VvPuPurchaseRequisitionMapper vvPuPurchaseRequisitionMapper;
    @Autowired
    private VvPuPurchaseRequisitionBMapper vvPuPurchaseRequisitionBMapper;

    @Transactional
    @XxlJob("RequisitionAdd")
    public void requisitionAdd(){
        List<VvPuPurchaseRequisition> vvPuPurchaseRequisitions = vvPuPurchaseRequisitionMapper.requisitionNotNullList();
        Header[] headers = ConnmeUtils.createHeaders();
        List<Map<String, Object>> datas = new ArrayList<>();
        for (VvPuPurchaseRequisition vvPuPurchaseRequisition : vvPuPurchaseRequisitions) {
            Map<String, Object> parameters = new HashMap<>();
            parameters.put("app_id", "64f00ef131adb70007ad6e6d");
            parameters.put("entry_id", "650ba4e4e5dec254a092b84c");
            List<JdyPurchaseOrder> jdyPurchaseOrders = PurchaseCommunity.queryBased(parameters, headers, vvPuPurchaseRequisition.getDocumentcode(),"documentcode");
            // 查询视图，这条数据是否存在
            int i1 = vvPuPurchaseRequisitionMapper.inquireIsExist(vvPuPurchaseRequisition.getPurchaserequisitionid());
            if (i1 == 0) {
                // 存入视图
                vvPuPurchaseRequisitionMapper.insertPurchaseRequisition(vvPuPurchaseRequisition);
                for (VvPuPurchaseRequisitionB vvPuPurchaseRequisitionB : vvPuPurchaseRequisition.getVvPuPurchaseRequisitionBS()) {
                    vvPuPurchaseRequisitionBMapper.insertPurchaseRequisitionB(vvPuPurchaseRequisitionB);
                }
            }
            // 查询是否已经存在简道云
            if (jdyPurchaseOrders.size() > 0) {
                continue;
            }
            datas.add( ConnmeUtils.convertEntityToMap(vvPuPurchaseRequisition));
        }
        final int batchSize = 50;  // 每批处理的数据量
        int dataSize = datas.size();
        int numberOfBatches = dataSize % batchSize == 0 ? dataSize / batchSize : dataSize / batchSize + 1;

        for (int batchIndex = 0; batchIndex < numberOfBatches; batchIndex++) {
            int startIndex = batchIndex * batchSize;
            int endIndex = Math.min(startIndex + batchSize, dataSize);

            List<Map<String, Object>> lista = datas.subList(startIndex, endIndex);

            Map<String, Object> parameter = new HashMap<>();
            parameter.put("app_id", "64f00ef131adb70007ad6e6d");
            parameter.put("entry_id", "650ba4e4e5dec254a092b84c");
            parameter.put("data_list", lista);

            String jsonSale = JSONUtil.toJsonStr(parameter);

        ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/batch_create", headers, jsonSale);
        }
    }
}
